/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fvsosp.telas;

import fvsosp.exemplar.Exemplar;
import fvsosp.exemplar.ExemplarRN;
import fvsosp.util.OnlyNumberField;
import fvsosp.util.Util;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author Pedro Saraiva
 */
public class TelaExemplarIndisponivel extends javax.swing.JDialog {

    ExemplarRN eRN = new ExemplarRN();
    Exemplar exemplar = null;

    /**
     * Creates new form TelaExemplarIndisponivel
     */
    public TelaExemplarIndisponivel() {
        initComponents();
        limpaCampos();
        tfTombo.setDocument(new OnlyNumberField());
        limpaCampos();
        setModal(true);
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        tfTombo = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel23 = new javax.swing.JLabel();
        btSair = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btPesquisar6 = new javax.swing.JButton();
        jLabel25 = new javax.swing.JLabel();
        chbDisponivel = new javax.swing.JCheckBox();
        jLabel27 = new javax.swing.JLabel();
        lblNExemplar = new javax.swing.JLabel();
        jLabel30 = new javax.swing.JLabel();
        jLabel31 = new javax.swing.JLabel();
        lblTitulo = new javax.swing.JLabel();
        chbAtivo = new javax.swing.JCheckBox();
        jLabel32 = new javax.swing.JLabel();
        lblTombo = new javax.swing.JLabel();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Exemplar Disponível");
        setModal(true);
        setResizable(false);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tfTombo.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfTombo.setToolTipText("Informe o tombo");
        tfTombo.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                tfTomboAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        tfTombo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfTomboKeyPressed(evt);
            }
        });
        jPanel2.add(tfTombo, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, 108, -1));

        jPanel3.setBackground(new java.awt.Color(59, 89, 152));
        jPanel3.setLayout(null);

        jLabel23.setFont(new java.awt.Font("Verdana", 1, 24)); // NOI18N
        jLabel23.setForeground(new java.awt.Color(255, 255, 255));
        jLabel23.setText("Exemplar Disponível");
        jPanel3.add(jLabel23);
        jLabel23.setBounds(0, 0, 320, 30);

        jPanel2.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 400, 35));

        btSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fvsosp/imagens/SA.png"))); // NOI18N
        btSair.setToolTipText("Sair");
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });
        jPanel2.add(btSair, new org.netbeans.lib.awtextra.AbsoluteConstraints(327, 183, -1, -1));

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fvsosp/imagens/save_1.png"))); // NOI18N
        btSalvar.setToolTipText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        jPanel2.add(btSalvar, new org.netbeans.lib.awtextra.AbsoluteConstraints(281, 183, 40, 33));

        btPesquisar6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fvsosp/imagens/application-form-magnify-icon.png"))); // NOI18N
        btPesquisar6.setToolTipText("Pesquisar");
        btPesquisar6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisar6ActionPerformed(evt);
            }
        });
        jPanel2.add(btPesquisar6, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 130, 34, -1));

        jLabel25.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel25.setText("Tombo.:");
        jPanel2.add(jLabel25, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, -1, 29));

        chbDisponivel.setBackground(new java.awt.Color(255, 255, 255));
        chbDisponivel.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        chbDisponivel.setText("Disponível");
        jPanel2.add(chbDisponivel, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 140, -1, -1));

        jLabel27.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        jLabel27.setText("Título.:");
        jPanel2.add(jLabel27, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 41, -1, 29));

        lblNExemplar.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        lblNExemplar.setText("Nº Exemplar.:");
        jPanel2.add(lblNExemplar, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 76, 100, 29));

        jLabel30.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        jLabel30.setText("Nº Exemplar.:");
        jPanel2.add(jLabel30, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 76, -1, 29));

        jLabel31.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        jLabel31.setText("Nº Exemplar.:");
        jPanel2.add(jLabel31, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 76, -1, 29));

        lblTitulo.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        lblTitulo.setText("Título.:");
        jPanel2.add(lblTitulo, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 41, 330, 29));

        chbAtivo.setBackground(new java.awt.Color(255, 255, 255));
        chbAtivo.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        chbAtivo.setText("Ativo");
        jPanel2.add(chbAtivo, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 140, -1, -1));

        jLabel32.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        jLabel32.setText("Tombo.:");
        jPanel2.add(jLabel32, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 69, 60, 40));

        lblTombo.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        lblTombo.setText("tombo");
        jPanel2.add(lblTombo, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 69, 100, 40));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
        );

        Util.setAcessibilidade(this);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
        dispose();
    }//GEN-LAST:event_btSairActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        if (lblNExemplar.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Informe o Tombo!", "OSBiblio", JOptionPane.INFORMATION_MESSAGE);
        } else {
            exemplar.setAtivo(chbAtivo.isSelected());
            if (chbDisponivel.isSelected()) {
                exemplar.setSituacao(1);
            } else {
                exemplar.setSituacao(4);
            }
            eRN.adiciona(exemplar);
            
            JOptionPane.showMessageDialog(rootPane, "Tombo: "+exemplar.getTombo()+ 
                    "\nExemplar: "+exemplar.getExemplar()+
                    "\nTítulo: "+exemplar.getAcervo().getTituloObra()+
                    "\nAlterado com sucesso!", "OSBiblio", JOptionPane.INFORMATION_MESSAGE);
            exemplar = null;
            limpaCampos();
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btPesquisar6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisar6ActionPerformed
        if (tfTombo.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Informe o Tombo!", "OSBiblio", JOptionPane.INFORMATION_MESSAGE);

        } else {
            eRN = new ExemplarRN();
            exemplar = eRN.pesquisarTombo(Short.valueOf(String.valueOf(tfTombo.getText())));
            if (exemplar == null) {
                JOptionPane.showMessageDialog(rootPane, "Exemplar não encontrado!", "OSBiblio", JOptionPane.INFORMATION_MESSAGE);
            } else {
                if (exemplar.getSituacao() == 1 || exemplar.getSituacao() == 4) {
                    lblNExemplar.setText(String.valueOf(exemplar.getExemplar()));
                    lblTitulo.setText(exemplar.getAcervo().getTituloObra());
                    lblTombo.setText(tfTombo.getText());
                    chbAtivo.setSelected(exemplar.isAtivo());
                    chbDisponivel.setSelected(exemplar.getSituacao() == 4 ? false : true);
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Exemplar encontra-se emprestado!", "OSBiblio", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
        tfTombo.setText("");
    }//GEN-LAST:event_btPesquisar6ActionPerformed

    private void tfTomboAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_tfTomboAncestorAdded
        // TODO add your handling code here:
    }//GEN-LAST:event_tfTomboAncestorAdded

    private void tfTomboKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfTomboKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            btPesquisar6ActionPerformed(null);
        }
    }//GEN-LAST:event_tfTomboKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaExemplarIndisponivel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaExemplarIndisponivel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaExemplarIndisponivel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaExemplarIndisponivel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaExemplarIndisponivel().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btPesquisar6;
    private javax.swing.JButton btSair;
    private javax.swing.JButton btSalvar;
    private javax.swing.JCheckBox chbAtivo;
    private javax.swing.JCheckBox chbDisponivel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lblNExemplar;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JLabel lblTombo;
    private javax.swing.JTextField tfTombo;
    // End of variables declaration//GEN-END:variables

    private void limpaCampos() {
        lblNExemplar.setText("");
        lblTitulo.setText("");
        lblTombo.setText("");
        chbAtivo.setSelected(false);
        chbDisponivel.setSelected(false);
        tfTombo.grabFocus();
    }
}
